package com.example.ytyproject.config;

import com.example.ytyproject.config.exception.AppException;
import com.example.ytyproject.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

public class SecurityUtil {

    public static String getUserId() {
        return getUser().getId();
    }

    public static User getUser() {
        try {
            Subject subject = SecurityUtils.getSubject();
            User principal = (User) subject.getPrincipal();
            return principal;
        } catch (Exception e) {
           throw new AppException("用户未登陆！");
        }
    }

    /**
     * 判断是否为管理员
     * @return
     */
    public static boolean isAdmin() {
        User user = getUser();
        if (user.getUsername().equals("admin")) {
            return true;
        }
        return false;
    }
}
